package com.csx.base.core.system;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2023/8/30 周三 13:33
 */
public class SystemTest {

    public static void main(String[] args) throws UnknownHostException {
        getHostAddress();
    }

    /**
     * 执行1000次,耗时0ms
     */
    public static void testGetSystemCurrentMillis() {

        long start = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            long l = System.currentTimeMillis();
        }

        long end = System.currentTimeMillis();
        System.out.println("执行耗时: " + (end - start) + " ms");
    }

    /**
     * 无论执行1次还是1000次，耗时都在13ms左右
     */
    public static void getHostAddress() throws UnknownHostException {
        long start1 = System.currentTimeMillis();
        InetAddress.getLocalHost().getHostAddress();
        long end1 = System.currentTimeMillis();
        System.out.println("第1执行耗时: " + (end1 - start1) + " ms");

        long start2 = System.currentTimeMillis();
        InetAddress.getLocalHost().getHostAddress();
        long end2 = System.currentTimeMillis();
        System.out.println("第2执行耗时: " + (end2 - start2) + " ms");

        long start3 = System.currentTimeMillis();
        InetAddress.getLocalHost().getHostAddress();
        long end3 = System.currentTimeMillis();
        System.out.println("第3执行耗时: " + (end3 - start3) + " ms");

        long start4 = System.currentTimeMillis();
        InetAddress.getLocalHost().getHostAddress();
        long end4 = System.currentTimeMillis();
        System.out.println("第4执行耗时: " + (end4 - start4) + " ms");
    }
}
